/***************************************************************************
 * Copyright 2012-2013 TXT e-solutions SpA
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * This work was performed within the IoT_at_Work Project
 * and partially funded by the European Commission's
 * 7th Framework Programme under the contract ICT-257367.
 *
 * Authors:
 *      Salvatore Piccione (TXT e-solutions SpA)
 *
 * Contributors:
 *        Domenico Rotondi (TXT e-solutions SpA)
 **************************************************************************/
package it.txt.ens.namespace;

import java.util.Dictionary;
import java.util.Properties;

/**
 * @author Salvatore Piccione (TXT e-solutions SpA - salvatore.piccione AT txtgroup.com)
 * @author Domenico Rotondi (TXT e-solutions SpA - domenico.rotondi AT txtgroup.com)
 *
 */
public class NamespaceInquirerFactoryImpl implements NamespaceInquirerFactory {

    public synchronized NamespaceInquirer create (Dictionary<String,?> properties) throws IllegalArgumentException {
        StringBuilder missingProperties = new StringBuilder();
        String dbURL = (String) properties.get(DATABASE_URL_ATTRIBUTE_ID);
        if (null != dbURL && dbURL.isEmpty())
            missingProperties.append(DATABASE_URL_ATTRIBUTE_ID);
        String usr = (String) properties.get(DATABASE_USERNAME_ATTRIBUTE_ID);
        if (null != usr && usr.isEmpty()) {
            if (missingProperties.length() > 0)
                missingProperties.append(", ");
            missingProperties.append(DATABASE_USERNAME_ATTRIBUTE_ID);
        }
        String psw = (String) properties.get(DATABASE_PASSWORD_ATTRIBUTE_ID);
        if (null != psw && psw.isEmpty()) {
            if (missingProperties.length() > 0)
                missingProperties.append(", ");
            missingProperties.append(DATABASE_USERNAME_ATTRIBUTE_ID);
        }
        if (missingProperties.length() > 0) {
            missingProperties.insert(0, "Missing properties: ");
            throw new IllegalArgumentException(missingProperties.toString());
        }
        return new NamespaceInquirerImpl(dbURL, usr, psw);
    }

    /* (non-Javadoc)
     * @see it.txt.ens.namespace.NamespaceInquirerFactory#create(java.util.Properties)
     */
    @Override
    public synchronized NamespaceInquirer create(Properties properties)
            throws IllegalArgumentException {
        StringBuilder missingProperties = new StringBuilder();
        String dbURL = properties.getProperty(DATABASE_URL_ATTRIBUTE_ID);
        if (null != dbURL && dbURL.isEmpty())
            missingProperties.append(DATABASE_URL_ATTRIBUTE_ID);
        String usr = properties.getProperty(DATABASE_USERNAME_ATTRIBUTE_ID);
        if (null != usr && usr.isEmpty()) {
            if (missingProperties.length() > 0)
                missingProperties.append(", ");
            missingProperties.append(DATABASE_USERNAME_ATTRIBUTE_ID);
        }
        String psw = properties.getProperty(DATABASE_PASSWORD_ATTRIBUTE_ID);
        if (null != psw && psw.isEmpty()) {
            if (missingProperties.length() > 0)
                missingProperties.append(", ");
            missingProperties.append(DATABASE_USERNAME_ATTRIBUTE_ID);
        }
        if (missingProperties.length() > 0) {
            missingProperties.insert(0, "Missing properties: ");
            throw new IllegalArgumentException(missingProperties.toString());
        }
        return new NamespaceInquirerImpl(dbURL, usr, psw);
    }

    /* (non-Javadoc)
     * @see it.txt.ens.namespace.NamespaceInquirerFactory#createConfiguration(java.util.Properties)
     */
    @Override
    public synchronized NamespaceInquirerConfiguration createConfiguration(
            Properties properties) throws IllegalArgumentException {
        StringBuilder missingProperties = new StringBuilder();
        String dbURL = properties.getProperty(DATABASE_URL_ATTRIBUTE_ID);
        if (null != dbURL && dbURL.isEmpty())
            missingProperties.append(DATABASE_URL_ATTRIBUTE_ID);
        String usr = properties.getProperty(DATABASE_USERNAME_ATTRIBUTE_ID);
        if (null != usr && usr.isEmpty()) {
            if (missingProperties.length() > 0)
                missingProperties.append(", ");
            missingProperties.append(DATABASE_USERNAME_ATTRIBUTE_ID);
        }
        String psw = properties.getProperty(DATABASE_PASSWORD_ATTRIBUTE_ID);
        if (null != psw && psw.isEmpty()) {
            if (missingProperties.length() > 0)
                missingProperties.append(", ");
            missingProperties.append(DATABASE_USERNAME_ATTRIBUTE_ID);
        }
        if (missingProperties.length() > 0) {
            missingProperties.insert(0, "Missing properties: ");
            throw new IllegalArgumentException(missingProperties.toString());
        }
        return new NamespaceInquirerConfiguration(dbURL, usr, psw);
    }

    /* (non-Javadoc)
     * @see it.txt.ens.namespace.NamespaceInquirerFactory#createConfiguration(java.util.Dictionary)
     */
    @Override
    public synchronized NamespaceInquirerConfiguration createConfiguration(
            Dictionary<String, ?> properties) throws IllegalArgumentException {
        StringBuilder missingProperties = new StringBuilder();
        String dbURL = (String) properties.get(DATABASE_URL_ATTRIBUTE_ID);
        if (null != dbURL && dbURL.isEmpty())
            missingProperties.append(DATABASE_URL_ATTRIBUTE_ID);
        String usr = (String) properties.get(DATABASE_USERNAME_ATTRIBUTE_ID);
        if (null != usr && usr.isEmpty()) {
            if (missingProperties.length() > 0)
                missingProperties.append(", ");
            missingProperties.append(DATABASE_USERNAME_ATTRIBUTE_ID);
        }
        String psw = (String) properties.get(DATABASE_PASSWORD_ATTRIBUTE_ID);
        if (null != psw && psw.isEmpty()) {
            if (missingProperties.length() > 0)
                missingProperties.append(", ");
            missingProperties.append(DATABASE_USERNAME_ATTRIBUTE_ID);
        }
        if (missingProperties.length() > 0) {
            missingProperties.insert(0, "Missing properties: ");
            throw new IllegalArgumentException(missingProperties.toString());
        }
        return new NamespaceInquirerConfiguration(dbURL, usr, psw);
    }

    /* (non-Javadoc)
     * @see it.txt.ens.namespace.NamespaceInquirerFactory#create(it.txt.ens.namespace.NamespaceInquirerConfiguration)
     */
    @Override
    public synchronized NamespaceInquirer create(NamespaceInquirerConfiguration config)
            throws IllegalArgumentException {
        return new NamespaceInquirerImpl(config.getURL(), config.getUsername(), config.getPassword());
    }

}
